import DB from "@/utils/sqlite.js";
import {
	toast
} from '@/utils/common'

//是否是唛头
export function isShippingMark(code) {
	var count = (code.match(/;/g) || []).length;
	if (count == 2) {
		return true;
	}
	return false;
}


//唛头在其他箱架中是否存在
export function shippingInTankRack(shippingNo, tank_rack_name) {
	return new Promise((resolve, reject) => {
		let open = DB.isOpen();
		if (open) {
			// 查询表 DB.selectTableData(表名,查询条件列名,查询条件列值)
			DB.selectTableData("shipping_mark", 'shipping_no', shippingNo)
				.then(res => {
					if (res && res.length > 0) {
						console.log('shipping_no已存在', JSON.stringify(res))
						if (res[0] && res[0]['tank_rack_name'] != tank_rack_name) {
							toast(`唛头${shippingNo}已在${res[0]['tank_rack_name']}箱架中~`)
							resolve(true)
						} else {
							resolve(false)
						}

					} else {
						resolve(false)
					}

				})
				.catch(error => {
					console.log("查询失败", error);
					reject(error)
				});
		} else {
			this.$modal.msg("数据库未打开");
			reject("数据库未打开")
		}
	})
}



//搜索已经存在的箱架
export function alreadyExistTankRack(tankRackName) {
	return new Promise((resolve, reject) => {
		let open = DB.isOpen();
		if (open) {
			// 查询表 DB.selectTableData(表名,查询条件列名,查询条件列值)
			DB.selectTableData("tank_rack", "name", tankRackName)
				.then(res => {
					console.log('res', res)
					if (res && res.length != 0 && res[0]) {
						console.log('res[0]', res[0]);
						resolve(res[0])
					}
				})
				.catch(error => {
					console.log("查询失败", error);
					reject(error)
				});
		} else {
			this.$modal.msg("数据库未打开");
			reject("数据库未打开")
		}
	})
}

export function formatting(str) {
	const dateObject = new Date(parseInt(str));
	const year = dateObject.getFullYear();
	const month = dateObject.getMonth() + 1;
	const day = dateObject.getDate();
	const hour = dateObject.getHours()
	const minute = dateObject.getMinutes()
	const dateString = `${year}-${month}-${day} ${hour}:${minute}`;
	console.log(dateString); // 输出：2022-8-10
	return dateString
}